@extends('layouts.app')
@section('content')
    <div class="content">
        @php
            use Illuminate\Support\Facades\Auth;
            use App\Modules\User\Models\Admin;
            $user = Auth::user();
        @endphp
        <div class="row custom-invoice">
            <div>
                <form class="form form-inline" method="get" action="{{ route('admin.rebate.index') }}" autocomplete="off">
                    <div class="form-group">
                        <input class="form-control" type="text" name="order_id" id="" value="{{ $orderId }}"
                               placeholder="订单ID">
                    </div>
                    <div class="form-group">
                        <input class="form-control" type="text" name="buyer" id="" value="{{ $buyer }}"
                               placeholder="购买人">
                    </div>
                    <div class="form-group">
                        <input class="form-control" type="text" name="beneficiary" id="" value="{{ $beneficiary }}"
                               placeholder="受益人">
                    </div>
                    <div class="form-group">
                        <input class="form-control" type="text" name="start" id="start" value="{{ $start }}"
                               placeholder="起始时间">
                    </div>
                    <div class="form-group">
                        <input class="form-control" type="text" name="end" id="end" value="{{ $end }}"
                               placeholder="结束时间">
                    </div>
                    <div class="form-group">
                        <select class="form-control" name="status">
                            <option>选择状态</option>
                            <option value="init" {{ $status== 'init'? 'selected':'' }}>待发放</option>
                            <option value="issue" {{ $status== 'issue'? 'selected':'' }}>可提现</option>
                            <option value="fail" {{ $status== 'fail'? 'selected':'' }}>未通过</option>
                            <option value="withdrawal" {{ $status== 'withdrawal'? 'selected':'' }}>提现中</option>
                            <option value="finish" {{ $status== 'finish'? 'selected':'' }}>已提现</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <!-- <select class="form-control" name="shop_id">
                            <option value="">选择门店</option>
                            @foreach($allShop as $shop)
                                <option
                                    value="{{ $shop->id }}" {{ $shopId == $shop->id?"selected" : "'" }}>{{ $shop->name }}</option>
                            @endforeach
                        </select> -->
                        <select class="form-control select2" name="shop_id" id="shop_id">
                            @if ($shopId != '')
                                <option value="{{$shopId}}">{{$shopName}}</option>
                            @else
                                <option value="">选择门店</option>
                            @endif
                        </select>
                    </div>
                    <button type="submit" class="btn btn-success" value="">搜索</button>
                    @if($user->is_merchant_staff == Admin::MERCHANT_AUTH)
                        @can('merchant_staff_rebate_export')
                            <button class="btn btn-primary btn-ms" type="submit" name="export" value="1">导出</button>
                        @endcan
                    @else
                        @can('rebate_export')
                            <button class="btn btn-primary btn-ms" type="submit" name="export" value="1">导出</button>
                        @endcan
                    @endif
                </form>
            </div>
            <br/>
            <div class="row">
                <div class="col-md-2" style="font-weight: bold">订单数量: {{$count}}</div>
                <div class="col-md-2" style="font-weight: bold">返现金额: {{$rebateMoney}}</div>
            </div>
            <hr/>
            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>订单ID</th>
                    <th>收益人</th>
                    <th>受益人头像</th>
                    <th>购买人</th>
                    <th>下单时间</th>
                    <th>返利金额</th>
                    <th>商家名称</th>
                    <th>状态</th>
                    <th>驳回原因</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @foreach($data as $item)
                    <tr>
                        <td>
                            <a href="{{ route('admin.order.detail', ['id' => $item->order_id])}}"
                               target="_blank"> {{ $item->order_id }}</a>
                        </td>
                        <td>{{ $item->beneficiary_name }}</td>
                        <td>
                            <img src="{{ $item->beneficiary_avatar }}" width="30"/>
                        </td>
                        <td>{{ $item->buyer }}</td>
                        <td>{{ $item->order_created_at }}</td>
                        <td>{{ $item->money/100 }}</td>
                        <td>
                            @if(!empty($shopList[$item->shop_id]))
                                {{ $shopList[$item->shop_id] }}
                            @endif
                        </td>
                        <td>
                            {{ $item->status == \App\Modules\Rebate\Models\Rebate::STATUS_FINISH ? \App\Modules\Rebate\Models\Rebate::$withdrawalStatusShow[$item->withdrawal_status] : \App\Modules\Rebate\Models\Rebate::$statusShow[$item->status] }}
                        </td>
                        <td>{{ $item->reject_reason }}</td>
                        <td>
                            @if($user->is_merchant_staff == Admin::MERCHANT_AUTH)
                                @can('merchant_staff_rebate_examine')
                                    @if($item->status == 'init')
                                        <a class="btn btn-primary btn-xs examine"
                                           data-url="{{ route('admin.rebate.del', ['id' => $item->id]) }}"
                                           data-reason="{{$item->reject_reason}}"><i class="fa fa-times"></i> 不通过</a>

                                        <a class="btn btn-primary btn-xs examine1"
                                            data-url="{{ route('admin.rebate.pass') }}"
                                            data-id = '{{ $item->id }}'
                                            ><i class="fa fa-check"></i> 通过</a>

                                    @elseif($item->status == 'fail')
                                        <a class="btn btn-primary btn-xs examine"
                                           data-url="{{ route('admin.rebate.del', ['id' => $item->id]) }}"
                                           data-reason="{{$item->reject_reason}}"><i class="fa fa-calendar-times-o"></i> 修改原因</a>
                                    @endif
                                @endcan
                                @can('merchant_staff_rebate_revoke')
                                    @if($item->status != 'finish' && $item->status != 'init')
                                        <a class="btn btn-primary btn-xs"
                                           href="{{ route('admin.rebate.revoke', ['id' => $item->id]) }}"><i
                                                class="fa fa-undo"></i> 撤回</a>
                                    @endif
                                @endcan
                            @else
                                @can('rebate-edit')
                                    @if($item->status == 'init')
                                        <a class="btn btn-primary btn-xs examine"
                                           data-url="{{ route('admin.rebate.del', ['id' => $item->id]) }}"
                                           data-reason="{{$item->reject_reason}}"><i class="fa fa-times"></i> 不通过</a>
                                        <a class="btn btn-primary btn-xs examine1"
                                            data-url="{{ route('admin.rebate.pass') }}"
                                            data-id = '{{ $item->id }}'
                                            ><i class="fa fa-check"></i> 通过</a>
                                    @elseif($item->status == 'fail')
                                        <a class="btn btn-primary btn-xs examine"
                                           data-url="{{ route('admin.rebate.del', ['id' => $item->id]) }}"
                                           data-reason="{{$item->reject_reason}}"><i class="fa fa-calendar-times-o"></i> 修改原因</a>
                                    @endif
                                @endcan
                                @can('rebate-revoke')
                                    @if($item->status != 'finish' && $item->status != 'init')
                                        <a class="btn btn-primary btn-xs"
                                           href="{{ route('admin.rebate.revoke', ['id' => $item->id]) }}"><i
                                                class="fa fa-undo"></i> 撤回</a>
                                    @endif
                                @endcan
                            @endif

                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            {{ $data->appends(["order_id" => $orderId, "buyer" => $buyer, "start" => $start, "end" => $end, "status" => $status, 'shop_id' => $shopId])->links() }}
        </div>

        <div class="modal fade loading custom-hide" id="hideForm" tabindex="-1" role="dialog"
             aria-labelledby="hideFormLibel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
            <div class="modal-dialog">
                <div class="content invoice" style="text-align: left">
                    <form class='form' action="" method="POST">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}"/>
                        <div class="form-group">
                            <label for="level" >驳回原因：</label>
                            <textarea class="form-control" required="required" name="reject_reason" type="text" value="" id="reject_reason" style="resize: none;" rows="8"></textarea>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-success form-control">提交</button>
                            <br/>
                            <br/>
                            <button type="button" class="btn" id="closeHideForm">关闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="modal fade loading custom-hide" id="hideForm1" tabindex="-1" role="dialog"
             aria-labelledby="hideFormLibel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
            <div class="modal-dialog">
                <div class="content invoice" >
                    <form class='form' action="" method="POST">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}"/>
                        <input type="hidden" name="rebate_id" id="rebate_id" value="">
                        <div class="form-group">
                            <label for="level" style="padding-right: 30px;">提现方式</label>
                            <input type="radio" value="0" name="withdrawal_type">立即提现
                            <input type="radio" value="1" name="withdrawal_type" checked style="margin-left: 20px;">稍后提现
                        </div>
                        <div class="form-group">
                            <label for="level" style="padding-right: 30px;">提现时间</label>
                            <input type="text"  name="withdrawal_time" id="withdrawal_time" value="" placeholder="选择时间" readonly style="background-color: white;">
                        </div>
                        <div class="form-group" style="text-align:center">
                            <button type="submit" class="btn btn-success">提交</button>
                            <button type="button" class="btn" id="closeHideForm1">关闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

    </div>
@endsection
@push('scripts')
    <script>

        $(".select2#shop_id").select2({
            width:"250px",
            ajax: {
                dataType: 'json',
                delay: 250,
                url: function () {
                    return '{{ route('admin.rebate.search_shops') }}';
                },
                processResults: function (data) {
                    return {
                        results: $.map(data, function (row, key) {
                            return {
                                id: key,
                                text: row,
                            };
                        })
                    };
                }
            }
        });

        $('#start').datetimepicker({format: "Y-m-d",timepicker:false});
        $('#end').datetimepicker({format: "Y-m-d",timepicker:false});

        $('#withdrawal_time').datetimepicker({
            format:'Y-m-d H:00:00',//时间格式
            minDate:0,//最小日期，当天
            maxDate:'+1970/01/15',//最大日期，15天后
            value:fun_date(7),
        });
        

        function fun_date(num) { 
            var date1 = new Date();
            //今天时间
            var time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate()
            var date2 = new Date(date1);
            date2.setDate(date1.getDate() + num);
            //num是正数表示之后的时间，num负数表示之前的时间，0表示今天
            var time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate(); 
            return time2+' 10:00:00';
        }
        
        // $('#start').attr('readonly', 'readonly');
        // $('#end').attr('readonly', 'readonly');
        $.datetimepicker.setLocale('zh');

        $('.examine').on('click', function () {
            $("#hideForm form").attr('action', $(this).attr('data-url'));
            $("#reject_reason").val($(this).attr('data-reason'))
            $("#hideForm").modal('show');
        });
        $('#closeHideForm').on('click', function () {
            $("#hideForm").modal('hide');
        });

        $('.examine1').on('click', function () {
            $("#hideForm1 form").attr('action', $(this).attr('data-url'));
            $("#rebate_id").val($(this).data('id'))
            $("#hideForm1").modal('show');
        });
        $('#closeHideForm1').on('click', function () {
            $("#hideForm1").modal('hide');
        });

        $('[name="withdrawal_type"]').on('click', function(){

            if($(this).val() == 1){
                // 稍后提现
                $('#withdrawal_time').attr('disabled', false);
            }else{
                // 立即提现
                $('#withdrawal_time').attr('disabled', true);
            }
        })
    </script>


@endpush
